<template>
  <view class="goods-item">
    <!-- 左侧的盒子 -->
    <view class="goods-item-left">
      <!-- 左边的勾选状态 -->
      <radio :checked="goods.goods_state_checked" color="#C00000" v-if="showRadio" @click="radioClickHandler"></radio>
      <image :src='goods.goods_small_logo || defaultPic'  class="goods-pic"></image>
    </view>
    <!-- 右侧的盒子 -->
    <view class="goods-item-right">
      <view class="goods-name">{{goods.goods_name}}</view>
      <view v-for="(value,key) in goods.goods_sizes" class="goods-sizes" >
        {{key}}:{{value}}
      </view>
      <view class="goods-info-box">
        <view class="goods-price">￥{{goods.goods_price | tofixed}}</view>
        
        <view class="goods-count" v-if="showCount">*{{goods.goods_count}}
        </view>
        
        <uni-number-box :min="0" :value="goods.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>

      </view>
    </view>
  </view>
</template>

<script>
  export default {
    
    props:{
      goods:{
        type:Object,
        default:{}
      },
      // 用于判断是否展示左侧勾选栏
      showRadio:{
        type:Boolean,
        // 默认不展示
        default:false
      },
      
      // 是否展示计数框
      showNum:{
        type:Boolean,
        default:false
      },
      
      showCount:{
        type:Boolean,
        default:false
      }
    },
    
    name:"my-goods",
    
    data() {
      return {
        //默认图片地址，防止图片失效
        defaultPic:'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    
    methods:{
      // 购物车左侧勾选
      radioClickHandler(){
        // 通过$emit触发radio-change函数，然后传两个参数
        this.$emit('radio-change',{
          goods_id : this.goods.goods_id,
          goods_state_checked : !this.goods.goods_state_checked
        })
      },
      
      // 购物车右下数量变化
      numChangeHandler(val){
        // console.log(val)
        this.$emit('num-change',{
          goods_id:this.goods.goods_id,
          // -0使其为数字
          goods_count:val - 0
        })
      }
    },
    
    filters:{
      // 将价格显示为两位小数
      tofixed(num){
        return Number(num).toFixed(2)
      }
    }
  }
</script>

<style lang="scss">
.goods-item{
  width: 750rpx;
  box-sizing: border-box;
  display: flex;
  padding:10px 5px;
  border-bottom:1px solid #f0f0f0;
  
  .goods-item-left{
    display: flex;
    margin-right: 5px;
    justify-content: space-between;
    align-items: center;
    
    .goods-pic{
      width: 100px;
      height: 100px;
      // 防止图片下面有间隙
      display: block;
    }
  }
  
  .goods-item-right{
    display: flex;
    flex:1;
    flex-direction: column;
    justify-content: space-between;
    
    .goods-name{
      font-size:13px;
    }
    
    .goods-sizes{
      font-size: 13px;
      display:flex;
      color:#808080;
    }
    
    .goods-info-box{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .goods-price{
        color:#c00000;
        font-size:16px;
      }
    }
  }
}

Page{
  background-color: white;
}
</style>